<?php

/**
 * listing_selection actions.
 *
 * @package    master_directory
 * @subpackage listing_selection
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class listing_selectionActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->listing = Doctrine_Core::getTable ( 'Listing' )->find(array($request->getParameter("list_id"))) ;
    
    $this->selection_components = $this->getSelectionComponents($this->listing);
    
    $table_headers = array();
    
    foreach ($this->listing->getListing2level() as $listing2level){
    	$level = $listing2level->getLevel();
    	array_push($table_headers, $level->getName());
    }
    
    $this->table_headers = $table_headers;
    
  	$previous_selection = Doctrine_Core::getTable('ListingSelection')->getByListing($this->listing->getId());
  		
  	$table_rows = array();
  	foreach ($previous_selection as $selection){
  		$table_row = array();
  		array_push($table_row, $selection->getId());
  		$previous_listing_selection_contents = Doctrine_Core::getTable('ListingSelection2categoryContent')->getByListingSelection($selection->getId());
  		foreach ($previous_listing_selection_contents as $previous_listing_selection_content){
  			$content = Doctrine_Core::getTable ( 'CategoryContent' )->find($previous_listing_selection_content->getCategoryContentId());
  			array_push($table_row, $content->getName());
  		}
  		
  		array_push($table_rows, $table_row);
  			
  	}
  	
  	 $this->table_rows = $table_rows;

    
  }
  
  
    public function executeLoadCategoryChildren(sfWebRequest $request){
    	
    	$category_content_id = $request->getParameter("id");
    	
    	$this->select_name = $request->getParameter("name");
        	
    	$this->contents = Doctrine_Core::getTable ( 'CategoryContent' )->getContentChildren($category_content_id);
    	
    }	
	  
  
  
  
	private function getSelectionComponents($listing){
 		
		$final_array = array();
  		
		foreach ($listing->getListing2level() as $listing2level){
			
			$component_element = array();
			
			$level = $listing2level->getLevel();
			
			$variable_name = array();
			
			$category = $level->getCategory();

			array_unshift($variable_name,$category->getName() . '.' . $level->getName());
			
			while($level->getParentId() != null){
				$level = Doctrine_Core::getTable('Level')->find($level->getParentId());
				$first_element = $variable_name[0];
				array_unshift($variable_name, $first_element . '.' . $level->getName());
			}

			$component_element[0] = $level;
			$component_element[1] = $variable_name;
			
			array_push($final_array, $component_element);
		}
		
		
		return $final_array;
		
  	}  
  	
  	
  	public function executeSaveListingSelection(sfWebRequest $request){
  		
  		$listing = Doctrine_Core::getTable ( 'Listing' )->find($request->getParameter("listing_id"));

  		
  		$content_ids = array();
  		foreach ($listing->getListing2level() as $listing2level){
  			
  			$level = $listing2level->getLevel();
  			$category = $level->getCategory();
  			$variable_name  = $category->getName() . '.' . $level->getName();
  			$variable_name_replaced  = str_replace(" ", "_", str_replace(".", "_", $variable_name));
  			$content_id = $request->getParameter($variable_name_replaced);
  			
  			array_push($content_ids, $content_id);				
  		}
  		
  		//create flag
  		$newListingSelection = new ListingSelection();
  		$newListingSelection->setListingId($listing->getId());
  		$newListingSelection->save();
  		
  		foreach ($content_ids as $content_id){
  			
	  		$newListing2CategoryContent = new ListingSelection2categoryContent();
  			
  			$newListing2CategoryContent->setListingSelectionId($newListingSelection->getId());
  		
  			$newListing2CategoryContent->setCategoryContentId($content_id);
  			
  			$newListing2CategoryContent->save();
  		}
  		
  		
	    
	    $selection_components = $this->getSelectionComponents($listing);
	    
	    $table_headers = array();
	    
	    foreach ($listing->getListing2level() as $listing2level){
	    	$level = $listing2level->getLevel();
	    	array_push($table_headers, $level->getName());
	    }
	    
	    
	  	$previous_selection = Doctrine_Core::getTable('ListingSelection')->getByListing($listing->getId());
	  		
	  	$table_rows = array();
	  	foreach ($previous_selection as $selection){
	  		$table_row = array();
	  		array_push($table_row, $selection->getId());
	  		$previous_listing_selection_contents = Doctrine_Core::getTable('ListingSelection2categoryContent')->getByListingSelection($selection->getId());
	  		foreach ($previous_listing_selection_contents as $previous_listing_selection_content){
	  			$content = Doctrine_Core::getTable ( 'CategoryContent' )->find($previous_listing_selection_content->getCategoryContentId());
	  			array_push($table_row, $content->getName());
	  		}
	  		
	  		array_push($table_rows, $table_row);
	  			
	  	}
	  	
  		return $this->renderPartial('listing_container', array('listing' => $listing, 'selection_components' => $selection_components, 'table_headers' => $table_headers, 'table_rows' => $table_rows));
  		
  	}
  	
  	public function executeDelete(sfWebRequest $request){
  		
  		$listing = Doctrine_Core::getTable ( 'Listing' )->find($request->getParameter("listing_id"));
  		
  		//delete_previous_selection
  		$previous_selection = Doctrine_Core::getTable('ListingSelection')->find($request->getParameter("selection_id"));
  		
  		$previous_listing_selection_contents = Doctrine_Core::getTable('ListingSelection2categoryContent')->getByListingSelection($previous_selection->getId());
  		foreach ($previous_listing_selection_contents as $previous_listing_selection_content){
  			$previous_listing_selection_content->delete();
  		}
  			
  		$previous_selection->delete();
  	
  		
  		
		return $this->redirect("listing_selection/index?list_id=" . $listing->getId() );	
  			
  	}
  
}
